// pages/ service/ service.js
const app = getApp()
var postLogin = require("../../utils/wh.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    distributions: [{
        img: '../../images/index/s4.png',
        starting: '鼓楼',
        endding: '仓山',
        pay: '200.00'
      },
      {
        img: '../../images/index/s4.png',
        starting: '鼓楼',
        endding: '仓山',
        pay: '200.00'
      },
      {
        img: '../../images/index/s4.png',
        starting: '鼓楼',
        endding: '仓山',
        pay: '200.00'
      },
      {
        img: '../../images/index/s4.png',
        starting: '鼓楼',
        endding: '仓山',
        pay: '200.00'
      },
    ],
    typeChoose: [{
        tit: '合作客户',
        checked: true
      },
      {
        tit: '即时客户'
      },
      {
        tit: '第三方同行接驳服务'
      },
    ],
    isActive: false,
    chooseImg: true,
    name: '',
    phone: '',
    remark: '合作客户',
    limit_num: 5, //每頁數量
    idxpage: 1, //第幾頁
    list: [], //數據列表
    no_data: 0,
    onearth: false, // 是否已经到达底部
    has:true
  },

  selectNav: function() {
    this.setData({
      isActive: false,
    })
  },
  selectNavs: function() {
    this.setData({
      isActive: true,
    })
  },
  choose: function(e) {
    let inx = e.currentTarget.dataset.index,
      typeChoose = this.data.typeChoose;
    for (let i = 0; i < typeChoose.length; i++) {
      this.data.typeChoose[i].checked = false;
    }
    if (typeChoose[inx].checked) {
      this.data.typeChoose[inx].checked = false;
    } else {
      this.data.typeChoose[inx].checked = true;
    }
    let userTypeChoose = typeChoose.filter((item) => {
      return item.checked
    })
    this.setData({
      typeChoose: typeChoose,
      remark: userTypeChoose[0].tit
    })
    console.log(userTypeChoose[0].tit)
  },
  detail: function(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: 'detail?id=' + id
    })
  },


  onLoad: function() {
    this.setData({
      isActive: false,
      name: '',
      phone: '',
      no_data: 0,
      idxpage: 1,
      limit_num: 5,
      onearth: false,
      list: [],
      typeChoose: [{
        tit: '合作客户',
        checked: true
      },
      {
        tit: '即时客户'
      },
      {
        tit: '第三方同行接驳服务'
      },
      ],
    })
    this.loop()
  },
  loop: function() {
    var that = this;
    // var code = app.globalData.code;
    // var url = module.exports.globalData.apiurl;
    // if (!code) {
    //   var that = this
    //   setTimeout(function() {
    //     that.loop();
    //   }, 100)
    // } else {
    //   that.setData({
    //     code: code,
    //   })
      var postUrl = '/api/company.transport/get_list',
        postData = {
          page: that.data.idxpage,
          page_num: that.data.limit_num,
        };
      postLogin.Post(postUrl, postData, that.case);
    // }
  },
  case: function(res) {
    console.log(res)
    let that = this;
    let page_size = this.data.limit_num,
      count = res.length,
      page = this.data.idxpage,
    has = this.data.has;
    var list = that.data.list.concat(res);
    if(list.length<=0){
      has=false
    }
    wx.hideLoading();
    if (page_size > count) {
      that.data.no_data = 1;
      if (list.length > 3) {
        that.setData({
          onearth: true
        })
      }
    }
    that.setData({
      list: list,
      has:has
    })
  },
  // 立即预约
  bindName: function(e) {
    this.setData({
      name: e.detail.value
    })
  },
  bindPhone: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  submitContent: function() {
    var name = this.data.name;
    var phone = this.data.phone;
    var remark = this.data.remark;
    if (!name || !phone) {
      wx.showToast({
        title: '请填写完整信息',
        icon: "none"
      })
      return false;
    }
    if (!(/^1[34578]\d{9}$/.test(phone))) {
      wx.showToast({
        title: '手机号格式错误',
        icon: "none"
      })
      return false;
    }
    var postUrl = '/api/company.transport/order',
      postData = {
        type: 2,
        name: this.data.name,
        mobile: this.data.phone,
        remark: this.data.remark,
      };
    postLogin.Post(postUrl, postData, this.submitsuccess);
  },
  submitsuccess: function() {
    wx.showToast({
      title: '提交成功',
      complete: function() {
        setTimeout(function() {
          let that = this
          wx.switchTab({
            url: '../index/index'
          })
        }, 2000);
      }
    })

    return;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    app.app_auth(this)
this.setData({
  isActive: false,
  chooseImg: true,
  name: '',
  phone: '',
  remark: '合作客户',
  has: true
})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    //下拉
    this.setData({
      no_data: 0,
      idxpage: 1,
      limit_num: 5,
      onearth: false,
      list: []
    });
    this.loop();
    wx.stopPullDownRefresh();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let no_data = this.data.no_data,
      page = this.data.idxpage,
      page_size = this.data.limit_num;
    if (no_data == 0) {
      this.data.idxpage = page + 1;
      this.loop();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})